﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Management;
using Microsoft.Win32;
using ZWL.DBUtility;
using DAL;
using Model.View;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();

        if (curBrowser.IndexOf("ie") < 0)
        {

            StrHelper.AjaxAlert(this, "请使用IE浏览器,否则样式会发生变化");
          
        }

        //需要真正清空登录用户时，请启用下面这句代码。考虑到部分用户不需要真正清空用户信息，只是转回登陆页。此处，留待自定义启用
        //ZWL.Common.PublicMethod.SetSessionValue("UserName", "");
        //验证序列号是否正确
        if (!Page.IsPostBack)
        {
            //try
            //{
            //    //当前序列号---------------------------------------------------------------------------
            //    string NowSerils = ZWL.DBUtility.DbHelperSQL.GetSHSL("select top 1 SerilsStr from ERPSerils");
            //    if (ZWL.Common.DEncrypt.DESEncrypt.Encrypt(GetMoAddress()).ToString() != NowSerils)
            //    {
            //        //序列号错误
            //        ZWL.Common.MessageBox.ShowAndRedirect(this, "验证系统序列号时发生错误！请重新获取序列号！", "SerilsSetting.aspx");
            //    }
            //}
            //catch
            //{
            //    ZWL.Common.MessageBox.ShowAndRedirect(this, "验证系统序列号时发生错误！请重新获取序列号！", "SerilsSetting.aspx");
            //}

            //try
            //{
            //    //时间字符串-----------------------------------------------------------------------------
            //    DateTime DateStr = DateTime.Parse(ZWL.Common.DEncrypt.DESEncrypt.Decrypt(ZWL.DBUtility.DbHelperSQL.GetSHSL("select top 1 DateStr from ERPSerils")));
            //    if (DateStr < DateTime.Now)
            //    {
            //        //使用时间到达
            //        ZWL.Common.MessageBox.ShowAndRedirect(this, "该序列号的使用时间已到，请重新获取序列号！", "SerilsSetting.aspx");
            //    }
            //}
            //catch
            //{
            //    ZWL.Common.MessageBox.ShowAndRedirect(this, "该序列号的使用时间已到，请重新获取序列号！", "SerilsSetting.aspx");
            //}

            ZWL.BLL.ERPDanWeiInfo MyModel = new ZWL.BLL.ERPDanWeiInfo();
            MyModel.GetModel();
            Page.Title = MyModel.DanWeiName;
            Session["Title"] = MyModel.DanWeiName;
        }
    }
    /**/
    /// <summary>
    /// 分析用户请求是否正常
    /// </summary>
    /// <param name="Str">传入用户提交数据</param>
    /// <returns>返回是否含有SQL注入式攻击代码</returns>
    public string ProcessSqlStr(string Str)
    {
        string SqlStr = "exec|insert|select|delete|update|count|chr|mid|master|truncate|char|declare";
        string ReturnValue = Str;
        try
        {
            if (Str != "")
            {
                string[] anySqlStr = SqlStr.Split('|');
                foreach (string ss in anySqlStr)
                {
                    if (Str.ToLower().IndexOf(ss) >= 0)
                    {
                        ReturnValue = "";
                    }
                }
            }
        }
        catch
        {
            ReturnValue = "";
        }
        if (Str.Length > 20)
        {
            ReturnValue = "";
        }
        return ReturnValue;
    }

    //获得网卡序列号----MAc地址
    public string GetMoAddress()
    {
        string MoAddress = " ";
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc2 = mc.GetInstances();
        foreach (ManagementObject mo in moc2)
        {
            if ((bool)mo["IPEnabled"] == true)
                MoAddress = mo["MacAddress"].ToString();
            mo.Dispose();
        }
        return MoAddress.ToString();
    }
    protected void ImgBtnOK_Click(object sender, ImageClickEventArgs e)
    {
        if (!CheckValidateCode(this.TxtValidateCode.Text))
        {
            StrHelper.AjaxAlert(this, "验证码输入错误！请重新输入！");
            return;
        }

        if (Page.IsValid)
        {

            string strPwdErr = this.TxtPassword.Text;
            DateTime LoginTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            string loginip = Request.UserHostAddress.ToString();
            string loginHostName = Request.UserHostName.ToString();
            DateTime adminlogintime;
            UserDAL bs = new UserDAL();
            string userName = StrHelper.ConvertSql(this.TxtUserName.Text.ToString());
            string userPwd = StrHelper.EncryptPassword(this.TxtPassword.Text.ToString(), StrHelper.PasswordType.MD5);
            if (bs.isLoginValidate(userName, userPwd))
            {

                Model.S_User s = bs.getByUserName(userName);
                SessionUser ss = new SessionUser();
                ss.UserName = s.UserName;
                ss.UserID = s.ID;
                ss.GroupID = s.GroupID;
                ss.Department = s.Department;
                ss.RoleId = s.RoleId;
                ss._RoleName = s.RoleName;
                ZWL.Common.Module.BaseLogin.SetSession(ss);//保存Session

                //S_User u = new S_User();
                //u = CRM.DAL.Factory.getUserDAL().getByUserName(userName);
                //if (u != null)
                //{

                //    adminlogintime = u.LoginTime;

                //    S_LoginLog g = new S_LoginLog();
                //    g.UName = userName;
                //    g.Pwd = "********";
                //    g.LoginTime = LoginTime;
                //    g.LoginIp = loginip;
                //    g.LoginHostName = loginHostName;
                //    g.LoginSuccess =1;
                //    CRM.DAL.Factory.getLogDAL().addLog(g);
                //    S_User user = new S_User();
                //    user.UserName = userName;
                //    user.LoginIp = loginip;
                //    user.LoginTime = adminlogintime;
                //    CRM.DAL.Factory.getUserDAL().UpdateUserIP(user);
                //    Response.Redirect("loading.htm");
                //}
                Response.Redirect("loading.htm");

            }
            else
            {

                //S_LoginLog gErr = new S_LoginLog();
                //gErr.UName = userName;
                //gErr.Pwd = strPwdErr;
                //gErr.LoginTime = LoginTime;
                //gErr.LoginIp = loginip;
                //gErr.LoginHostName = loginHostName;
                //gErr.LoginSuccess = 0;
                //CRM.DAL.Factory.getLogDAL().addErrLog(gErr);
                StrHelper.AjaxAlert(this, "登录失败！请检查用户名和密码");
                return;
            }
        }

        //if (!CheckValidateCode(this.TxtValidateCode.Text))
        //{
        //    StrHelper.AjaxAlert(this, "验证码输入错误！请重新输入！");
        //    return;
        //}

        //if (Page.IsValid)
        //{


        //    ZWL.BLL.ERPUser MyUser = new ZWL.BLL.ERPUser();
        //    if (ConfigurationManager.AppSettings["OALogin"].ToString().Trim() == "0")
        //    {
        //        string UserNameStr = ZWL.DBUtility.DbHelperSQL.GetSHSL("select top 1 UserName from ERPUser where TrueName='" + ProcessSqlStr(TxtUserName.Text.Trim()) + "'");
        //        MyUser.UserLogin(UserNameStr, ZWL.Common.DEncrypt.DESEncrypt.Encrypt(TxtPassword.Text));
        //        MyUser.UserLogin(UserNameStr, TxtPassword.Text);

        //    }
        //    else
        //    {
        //        MyUser.UserLogin(TxtUserName.Text.Trim(), ZWL.Common.DEncrypt.DESEncrypt.Encrypt(TxtPassword.Text));
        //        MyUser.UserLogin(TxtUserName.Text.Trim(), TxtPassword.Text);

        //    }
        //}
    }    /// <summary>
    /// 判断验证码是否符合要求
    /// </summary>
    /// <param name="code">用户输入的验证码</param>
    /// <returns>返回验证码是否正确</returns>

    public bool CheckValidateCode(string code)
    {
        try
        {
            if (code.ToUpper() != Session["CheckCode"].ToString().ToUpper()) return false;
            return true;
        }
        catch (Exception e)
        {
            e.ToString();
            return false;
        }
    }
}
